package org.android.brasil.projetos.oktoberfest2011.view;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.view.controller.HomeController;
import org.android.brasil.projetos.oktoberfest2011.view.controller.WidgetController;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.RemoteViews;

public class OktoberfestWidget2x1View extends AppWidgetProvider {

	public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
	public static String ACTION_OPEN_ACTIVITY = "ActionActivityOpen";

	private static RemoteViews views;

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		int N = appWidgetIds.length;

		// Faz um loop para cada App Widget que pertence a esse provider
		for (int i = 0; i < N; i++) {
			int appWidgetId = appWidgetIds[i];
			updateWidget(context, appWidgetId);
		}
		WidgetController.agendarAtualizacaoDiaria(context, ACTION_WIDGET_RECEIVER);
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		super.onReceive(context, intent);

		// Atualiza a widget clicada com uma nova fortune
		if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
			Bundle extras = intent.getExtras();
			if (extras == null)
				return;

			// Busca o ID do widget nos extras para atualizar apenas o widget
			// clicado
			int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);

			if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
				return;
			updateWidget(context, widgetId);
		}

	}

	public static void updateWidget(Context context, int appWidgetId) {
		// Intent para setar a action Receiver e um Extra com o ID do widget
		Intent intentUpdate = new Intent(context, OktoberfestWidget2x1View.class);
		intentUpdate.setAction(ACTION_WIDGET_RECEIVER);
		intentUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

		views = new RemoteViews(context.getPackageName(), R.layout.oktoberfest_widget_2x1_view);

		Bitmap bMap = null;

		int dias = WidgetController.getDiasParaEvento();
		//muda as frases do widget

		if(dias < 0){
			//dias = dias * (-1);
			dias = WidgetController.getDiasDeEvento();
			
			views.setTextViewText(R.idWidget2x1.tv_dias, context.getResources().getQuantityString(R.plurals.days, dias, dias));
			views.setTextViewText(R.idWidget2x1.tv_faltam, context.getResources().getString(R.string.alemao_ha));

		}
		else{
			views.setTextViewText(R.idWidget2x1.tv_dias, context.getResources().getQuantityString(R.plurals.days, dias, dias));
			views.setTextViewText(R.idWidget2x1.tv_faltam, context.getResources().getQuantityString(R.plurals.missing, dias, dias));
		}
		
		int unidades = dias / 10;

		bMap = BitmapFactory.decodeResource(context.getResources(),
				WidgetController.escolherImagemNumeroDia(unidades));

		views.setImageViewBitmap(R.idWidget2x1.img_dezenas, bMap);

		int dezenas = dias % 10;

		bMap = BitmapFactory.decodeResource(context.getResources(),
				WidgetController.escolherImagemNumeroDia(dezenas));

		views.setImageViewBitmap(R.idWidget2x1.img_unidades, bMap);

		Intent intentOpen = new Intent(context, HomeController.class);
		intentOpen.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

		PendingIntent actionPendingIntentOpen = PendingIntent.getActivity(context, appWidgetId,
				intentOpen, PendingIntent.FLAG_UPDATE_CURRENT);

		views.setOnClickPendingIntent(R.idWidget2x1.layout_appwidget, actionPendingIntentOpen);


		if(dias == 1){
			views.setTextViewText(R.idWidget2x1.tv_dias, context.getResources().getString(R.string.dia));	
		}else{
			views.setTextViewText(R.idWidget2x1.tv_dias, context.getResources().getString(R.string.dias));
		}
		AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, views);

	}
}